package com.xy.shop.common.exception;

import com.xy.shop.common.base.Codes;
import com.xy.shop.common.base.ResponseResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 **/
@ControllerAdvice
public class GlobalExceptionHandler {


    @ResponseBody
    @ExceptionHandler(value = OperationException.class)
    public ResponseResult operationExceptionHandler(OperationException oe){
        return new ResponseResult(oe.getCode(), oe.getMsg());
    }

    @ResponseBody
    @ExceptionHandler(value = NoHandlerFoundException.class)
    public ResponseResult noHandlerFoundException(NoHandlerFoundException e){
        return ResponseResult.error(Codes.SYSTEM_ERROR);
    }

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResponseResult defaultExceptionHandler(Exception e){
        return ResponseResult.error(Codes.SYSTEM_ERROR);
    }
}
